package com.his.struts.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.diseaseInfo.DiseaseInfoDao;
import com.his.struts.dao.diseaseInfo.DiseaseInfoImp;
import com.his.struts.form.DiseaseForm;
import com.his.struts.form.DiseaseInfoForm;
import com.his.struts.form.UserForm;
import com.his.struts.util.PageUtil;

/** 
 * MyEclipse Struts
 * Creation date: 07-14-2011
 * 
 * XDoclet definition:
 * @struts.action path="/diseaseInfo" name="diseaseInfoForm" parameter="method" scope="request" validate="true"
 */
public class DiseaseInfoAction extends DispatchAction {


	DiseaseInfoDao dao = new DiseaseInfoImp();

	/** 
	 * Method addDiseaseInfo 添加个人病例详情表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward addDiseaseInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DiseaseInfoForm diseaseInfoForm = (DiseaseInfoForm) form;
		// 得到session中的token
		String token = (String)request.getSession().getAttribute("token");
		// 得到网页中的token
		String newToken = request.getParameter("token");
		// 是否重复提交
		if(newToken == null || !newToken.equals(token)){
			// 添加基本信息
			dao.addDiseaseInfo(diseaseInfoForm);
		}
		// 把最新token设置到session中
		request.getSession().setAttribute("token", newToken);
		request.setAttribute("message", "保存成功");
		return mapping.findForward("getDiseaseInfo");
	}
	
	/** 
	 * Method addDiseaseInfo 添加个人病例详情表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward delDiseaseInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DiseaseInfoForm diseaseInfoForm = (DiseaseInfoForm) form;
		request.setAttribute("message", "删除成功");
		return mapping.findForward("getDiseaseInfo");
	}
	
	/** 
	 * Method addDiseaseInfo 添加个人病例详情表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward updateDiseaseInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DiseaseInfoForm diseaseInfoForm = (DiseaseInfoForm) form;
		// 得到session中的token
		String token = (String)request.getSession().getAttribute("token");
		// 得到网页中的token
		String newToken = request.getParameter("token");
		// 是否重复提交
		if(newToken == null || !newToken.equals(token)){
			// 修改基本信息
			dao.updateDiseaseInfo(diseaseInfoForm);
		}
		// 把最新token设置到session中
		request.getSession().setAttribute("token", newToken);
		
		request.setAttribute("message", "更新成功");
		return mapping.findForward("getDiseaseInfo");
	}
	
	
	/** 
	 * Method getDiseaseInfo 得到个人病例详情表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getDiseaseInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DiseaseInfoForm diseaseInfoForm = (DiseaseInfoForm) form;
		DiseaseInfoForm disform=null;
		String id=request.getParameter("did");
		if (id == null) {
			id = "";
			UserForm uform=(UserForm)request.getSession().getAttribute("userinfo");
			if(uform==null){
				return mapping.findForward("login");
			}
			int uid =uform.getUid();
			disform =dao.getDiseaseInfo(uid);
			request.setAttribute("disform",disform);
			return mapping.findForward("success");
		} else {
			int uid = Integer.parseInt(id);
			 disform =dao.getDiseaseInfo(uid);
			 request.setAttribute("disform",disform);
			 return mapping.findForward("look");
		}
		
	}
	
	
	/** 
	 * Method getAllDiseaseInfo 得到所有病例详情表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getAllDiseaseInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DiseaseInfoForm diseaseInfoForm = (DiseaseInfoForm) form;
		String id=request.getParameter("did");
		if (id == null) {
			id = "";
		} else {
			int uid = Integer.parseInt(id);
			dao.delDiseaseInfo(uid);
		}
		String sql ="dis_agodistime as a " +
					"join dis_basicinfo as b " +
					"join dis_estimate as c " +
					"join dis_female as d " +
					"join dis_fimily as e " +
					"join dis_hurt as f " +
					"join dis_living as g " +
					"join dis_yinshi as h " +
					"where a.uid = b.uid " +
					"and b.uid = c.uid " +
					"and c.uid = d.uid " +
					"and d.uid = e.uid " +
					"and e.uid = f.uid " +
					"and f.uid = g.uid " +
					"and g.uid = h.uid ";

		ResultSet rs = PageUtil.getPage(sql, 10, request);

		// 如果数据不为空
		if (rs != null) {
			// 调用DAO 得道所有病例
			List<DiseaseInfoForm> disList = dao.getAllDiseaseInfo(rs);
			// 数据放到request中
			request.setAttribute("disList", disList);
		}
		return mapping.findForward("all");
	}
}